package com.zeng.generate.code.util;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.FileUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.simple.SimpleDataSource;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.StaticLog;
import cn.hutool.poi.excel.ExcelWriter;
import com.zeng.generate.code.other.ReportFields;
import com.zeng.generate.code.other.UserBase;
import com.zeng.generate.code.callback.ExportImagePageCallBack;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Comparator;
import java.util.List;

public class UserBaseExportSvc {

    public static ExcelWriter exportUserBaseVo(List<?> userBaseExcelVos, List<ReportFields> reportFields, Integer row)  {
        reportFields.sort(Comparator.comparing(ReportFields::getSortNum));
        return ExportExcelUtil.excelPageDownload(userBaseExcelVos, row, (writer) -> {
            userBaseDefaultTitle(writer, reportFields,3);
        }, (writer) -> {
            userBaseDefaultStyle(writer, reportFields);
        }, (ExportImagePageCallBack<?>) (writer, list) -> {
            for (int i = 0; i < list.size(); i++) {
                writer.getOrCreateRow(i+1).setHeightInPoints(30);
            }
        });
    }

    private static void userBaseDefaultStyle(ExcelWriter writer, List<ReportFields> reportFields){
        ExportExcelUtil.defaultStyle(writer);

        for (ReportFields field : reportFields) {
            if (field.getWidth() > 0) {
                writer.getSheet().setColumnWidth(field.getSortNum(), field.getWidth() * 256);
            }
        }
    }

    private static void userBaseDefaultTitle(ExcelWriter writer, List<ReportFields> reportFields, Integer languageType)  {
        writer.setOnlyAlias(true);

        Integer language = Convert.toInt(languageType,1);

        for (int i = 0; i < reportFields.size(); i++) {
            ReportFields field = reportFields.get(i);
            //System.out.println(field.getSortNum()+ " - " +field.getName()+ " - "+field.getField());

            if (JSONUtil.isJson(field.getName())) {
                field.setName(JSONUtil.parseObj(field.getName()).getStr(language.toString(), "Unknown "+i));
            }

            writer.addHeaderAlias(field.getFields(), field.getName());
        }
    }

   // @Test
    public void test() {
        String tableName = new String[]{"tb_report_fields","tb_user_dict"}[0];
        DataSource ds = new SimpleDataSource("jdbc:mysql://192.168.10.129:3306/hrss", "root", "dksh@123");

        try {

            List<ReportFields> reportFields = Db.use(ds).findAll(Entity.create(tableName), ReportFields.class);

            List<UserBase> entityList = Db.use(ds).findAll(Entity.create("tb_user_base"), UserBase.class);

            ExcelWriter writer = exportUserBaseVo(entityList, reportFields, 9999);

            writer.flush(FileUtil.file("d:/writeBeanTest.xlsx"));

            //关闭writer，释放内存
            writer.close();

            StaticLog.info("{}", reportFields);
            StaticLog.info("{}", entityList);

        } catch (SQLException e) {
            StaticLog.error(e.getMessage(), e);
        }
    }

}
